// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import { DemoUserPromiseClient as Client } from '@demo/proto/user/demo_user_grpc_web_pb'

import { ComQuery as ComClass } from '@demo/proto/common/ws_common_pb'
import {
  ListReq as ListClass,
  DetailReq as OneClass,
  SubmitReq as AddClass,
  SubmitReq as EditClass,
  DetailReq as DelClass,
  StatusReq as PassClass,
  StatusReq as RejClass,
} from '@demo/proto/user/demo_user_pb'
import { UserDto as BaseClass } from '@demo/proto/user/demo_user_base_pb'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const BASE = [BaseClass, 'content']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM, BASE)
    const resp = await CORE.grpc(TIP, CLIENT, 'query', DTO)
    return [resp.comQuery.totalCount, resp.contentList || []]
  },
  transList(arr, { state: { STATUS } }) {
    arr.forEach((item, i) => {
      item.statusName = CORE.dict(STATUS, item.status)
      item.id = item.id || i
    })
    return arr
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询详情') {
    const DTO = CORE.grpcDTO(OneClass, { id })
    const resp = await CORE.grpc(TIP, CLIENT, 'detail', DTO)
    return resp.content || {}
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'create', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'update', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { id })
    await CORE.grpc(TIP, CLIENT, 'delete', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 通过 —————————————————————————————— //

  async pass({ id }, TIP = '通过') {
    const DTO = CORE.grpcDTO(PassClass, { id, status: 1 })
    await CORE.grpc(TIP, CLIENT, 'status', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 拒绝 —————————————————————————————— //

  async rej({ id }, TIP = '拒绝') {
    const DTO = CORE.grpcDTO(RejClass, { id, status: 2 })
    await CORE.grpc(TIP, CLIENT, 'status', DTO)
    return CORE.grpcOK(TIP)
  },
}
